﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dukous.BizService.Implements;
using Dukous.Security.BizService;
using Dukous.Security.Dao;
using Dukous.Security.Domain;
using Dukous.Util;
using Spring.Transaction.Interceptor;

namespace Dukous.Security.BizService.Implements
{
    public class SecurityService : EntityBizService<User, long, IUserDao>, ISecurityService
    {
        /// <summary>
        /// 
        /// </summary>
        public ILogonInfoDao LogonInfoDao { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public ISecurityObjectDao SecurityObjectDao { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public ISecurityObjectPermissionDao SecurityObjectPermissionDao { get; set; }

        #region ISecurityService 成员

        User this[string index] 
        {
            get
            {
                if (string.IsNullOrEmpty(index))
                {
                    return null;
                }

                string[] s = index.Split('|');

                if (s.Length != 2)
                {
                    return null;
                }

                LogonInfo logonInfo = this.LogonInfoDao.Find(new Guid(s[0]), s[1]);
                if (logonInfo == null)
                {
                    return null;
                }

                return logonInfo.IsOff ? null : logonInfo.User;
            }
        }

        User ISecurityService.this[string index]
        {
            get { return this[index]; }
        }

        /// <summary>
        /// 尝试登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="ip"></param>
        /// <returns></returns>
        public LogonInfo TryLogon(string userName, string password, string ip)
        {
            User user = this.EntityDao.FindByUserName(userName);
            if (user == null)
            {
                return null;
            }

            if (user.Password != MD5Util.Encrypt(password))
            {
                return null;
            }

            return new LogonInfo(Guid.NewGuid(), user, ip);
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="logonInfo"></param>
        /// <returns></returns>
        [Transaction(ReadOnly=false)]
        public string DoLogon(LogonInfo logonInfo)
        {
            logonInfo.RefreshTime = DateTime.Now;

            this.LogonInfoDao.SaveOrUpdate(logonInfo);

            return logonInfo.Guid.ToString();
        }

        /// <summary>
        /// 注销
        /// </summary>
        /// <param name="logonInfo"></param>
        public void Logoff(LogonInfo logonInfo)
        {
            logonInfo.IsOff = true;
            logonInfo.RefreshTime = DateTime.Now;
            this.LogonInfoDao.SaveOrUpdate(logonInfo);
        }

        /// <summary>
        /// 获取登录信息
        /// </summary>
        /// <param name="guid"></param>
        /// <param name="ip"></param>
        /// <returns></returns>
        public LogonInfo FindLogonInfo(Guid guid, string ip)
        {
            return this.LogonInfoDao.Find(guid, ip);
        }

        public bool CheckPermission(User user, string obj, string permission)
        {
            return CheckPermission(user, obj, null, permission);
        }

        public bool CheckPermission(User user, string obj, string field, string permission)
        {
            if (string.IsNullOrEmpty(obj) || user == null || string.IsNullOrEmpty(permission))
            {
                return false;
            }
            SecurityObject securityObject = this.SecurityObjectDao.FindByFullName(obj);
            if (securityObject == null)
            {
                return false;
            }

            return securityObject.Check(user, permission, field);
        }

        public bool CheckPermission(Group group, string obj, string permission)
        {
            return CheckPermission(group, obj, null, permission);
        }

        public bool CheckPermission(Group group, string obj, string field, string permission)
        {
            if (string.IsNullOrEmpty(obj) || group == null || string.IsNullOrEmpty(permission))
            {
                return false;
            }
            SecurityObject securityObject = this.SecurityObjectDao.FindByFullName(obj);
            if (securityObject == null)
            {
                return false;
            }

            return securityObject.Check(group, permission, field);
        }

        public SecurityObjectPermission FindSecurityObjectPermissionById(long id)
        {
            return this.SecurityObjectPermissionDao.FindById(id);
        }

        public IList<SecurityObjectPermission> SelectAllSecurityObjectPermissions()
        {
            return this.SecurityObjectPermissionDao.SelectAll().OrderBy(x=>x.SecurityObject.Id).ToList();
        }

        #endregion
    }
}
